import json, urllib
from urllib.parse import urlencode
from urllib.request import urlopen


def main():
    appkey = ''
    requests1(appkey, "POST")
    requests2(appkey, "GET")


def requests1(appkey, m="POST"):
    url = "http://op.juhe.cn/vercode/index"
    params = {
        "key": appkey,
        "codeType": "",
        "image": "",
        "dtype": "",
    }
    params = urlencode(params)
    if m == "GET":
        f = urllib.request.urlopen(url, params)
    content = f.read()
    res = json.loads(content)
    if res:
        error_code = res["error_code"]
        if error_code == 0:
            print(res["result"])
        else:
            print("%s:%s" % (res["error_code"], res["reason"]))
    else:
        print("requests api error")


def requests2(appkey, m="GET"):
    url = "http://op.juhe.cn/vercode/codeType"
    params = {
        "key": appkey,
        "dtype": "json",
    }
    params = urlencode(params)
    if m == "GET":
        f = urllib.request.urlopen("%s?%s" % (url, params))
    content = f.read()
    res = json.loads(content)
    if res:
        error_code = res["error_code"]
        if error_code == 0:
            print(res["result"])
        else:
            print("%s:%s" % (res["eooro_code"], res["reason"]))
    else:
        print("request api error")


if __name__ == '__main__':
    main()
